package org.kk.ds.queue;

import java.util.NoSuchElementException;

/**
 * 实现数据结构: 队列
 * @author kk
 * @Date 2021/2/4
 */
public class QueueDemo {
    private Integer[] data;
    private int length;
    private int qf,qt; //队头和队尾
    public QueueDemo(){
        this(10);
    }
    public QueueDemo(int size){
        this.data = new Integer[size];
    }
    public int size(){
        return length;
    }
    public boolean isEmpty(){
        return length == 0;
    }
    public void push(int i){
        if(data.length == size()){
            expansion(size() << 1);
        }
        data[qt++] = i;
        length++;
    }
    public int pop(){
        if (isEmpty()) throw new NoSuchElementException();
        int result = data[qf];
        for (int i = 0; i < size(); i++){
            data[i] = data[i + 1];
        }
        length--;
        return result;
    }
    public void expansion(int index){
        if(index < size()) return;
        Integer[] temp = new Integer[index];
        int j = qt;
        for (int i = 0; i < size(); i++){
            temp[i] = data[j++];
        }
        data = temp;
    }
}
